// pages/shopdetaillist/shopdetaillist.js
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    form: {
      id: '',
      name: '',
      barcode: '',
      price: '',
      unit: '',
      categoryName: '',
      categoryId: '',
      sort:'',
      diagram: ''
    },
    categoryList: [
      { name: '饮料', id: 1 },
      { name: '零食', id: 2 },
      { name: '日用品', id: 3 }
    ],
    showCategoryPicker: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.getShopList(options.id,options.name,options.status)
  },

  chooseShop(){
    const that = this
    wx.navigateTo({
      url: '/pages/shopall/shopall',
      events: {
        acceptDataFromOpenedPage(data) {
          console.log('回传数据', data.item)
          that.setData({ 'form.name': data.item.name,'form.id': data.item.id,'form.price': data.item.price,'form.barcode': data.item.barcode,'form.sort': data.item.sort,'form.diagram': data.item.diagram });
        }
      }
    });
  },

  getShopList(id,name,statue){
    this.requestHttp('/app/client/mine/goods/category', {pageNum:1,pageSize:10,categoryId:id,name:name,statue:statue}, 'post').then((data) => {
      console.log(data)
      this.setData({
        list:data.rows
      })
      console.log(this.list)
    })
  },

  onInputName(e) {
    this.setData({ 'form.name': e.detail.value });
  },

  onInputBarcode(e) {
    this.setData({ 'form.barcode': e.detail.value });
  },

  onInputPrice(e) {
    this.setData({ 'form.price': e.detail.value });
  },

  onInputUnit(e) {
    this.setData({ 'form.unit': e.detail.value });
  },

  onSelectCategory() {
    this.setData({ showCategoryPicker: true });
  },

  onCategoryChange(e) {
    const index = e.detail.value;
    const selected = this.data.categoryList[index];
    this.setData({
      'form.categoryId': selected.id,
      'form.categoryName': selected.name,
      showCategoryPicker: false
    });
  },

  onChooseImage() {
    wx.chooseMedia({
      count: 1,
      mediaType: ['image'],
      sourceType: ['album', 'camera'],
      success: (res) => {
        this.setData({ 'form.image': res.tempFiles[0].tempFilePath });
      }
    });
  },

  onScan() {
    wx.scanCode({
      onlyFromCamera: false,
      success: (res) => {
        this.setData({ 'form.barcode': res.result });
      }
    });
  },

  onSubmit() {
    console.log('提交数据：', this.data.form);
    // 调接口提交数据...
    this.requestHttp('/app/client/manage/goods/add', {pageNum:1,pageSize:10,categoryId:this.data.form.categoryId,name:this.data.form.name,statue:"enable"}, 'post').then((data) => {
      console.log(data)
      if (data.data.code === 0) {
        wx.showToast({
          title: '提交成功',
        })
      } else if (data.data.code === 500) {
        wx.showToast({
          title: data.data.msg,
        })
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  },
  requestHttp(url,data,method){
    return new Promise(function (reslove, reject) {
      let headers = {
        'Authorization': app.globalData.token || '',
        'Content-Type': 'application/json'
      };
      wx.request({
        url: app.globalData.host + url,
        method: method || 'get',
        data: data,
        header: headers,
        success: function success(res) {
          // const data = res.data;
          // if (data.code * 1 === 0) {
          //   reslove(data)
          // } else {
          //   wx.showToast({
          //     title: data.msg || '网络异常',
          //     icon: 'error',
          //   })
          //   setTimeout(() => {
          //     reject(data);
          //   }, 10)
          // }
          reslove(res)
        },
        fail: function fail(res) {
          wx.showToast({
            title: res,
            icon: 'error',
          })
          reject(res);
        }
      })
    })
  }
})